package myky.com.doctorapp.ui

import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.github.nukc.stateview.Constant
import com.gyf.barlibrary.ImmersionBar
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter
import com.zhy.view.flowlayout.TagFlowLayout
import kotlinx.android.synthetic.main.activity_find_docotr_home_page.*
import kotlinx.android.synthetic.main.find_doctor_header.*
import kotlinx.android.synthetic.main.find_doctor_header.view.*
import kotlinx.android.synthetic.main.public_common_title.*
import myky.com.doctorapp.R
import myky.com.doctorapp.adapter.FindDoctorDoctorAdapter
import myky.com.doctorapp.base.BaseActivity
import myky.com.doctorapp.contract.FindDoctorContract
import myky.com.doctorapp.entity.DepatmentWrapperEntity
import myky.com.doctorapp.entity.DoctorWrapperEntity
import myky.com.doctorapp.presenter.FindDoctorPresenter
import myky.com.doctorapp.utils.ToastUtils


/**
 * Title:
 * Description: 找医生首页
 * @author xiangyao
 * date 2018/12/17
 */
class FindDocotrHomePageActivity : BaseActivity<FindDoctorPresenter, DepatmentWrapperEntity>(), FindDoctorContract.View, View.OnClickListener, BaseQuickAdapter.OnItemChildClickListener {

    private lateinit var doctorAdapter: FindDoctorDoctorAdapter
    private var mDoctorList: MutableList<DoctorWrapperEntity.DoctorListEntity.DoctorsEntity>? = null
    private lateinit var headView: View
    private lateinit var mFlowLayout: TagFlowLayout
    private var nowCurrent = 1

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {

        when (view?.id) {
            R.id.ll_doctor_details_top -> {
                val data = adapter?.data as List<*>

                val mDoctorsEntity = data[position] as DoctorWrapperEntity.DoctorListEntity.DoctorsEntity

                ToastUtils.show(mDoctorsEntity.realname)
            }
            R.id.item_smallbtn_consoult -> {
                ToastUtils.show("专家咨询")
            }
            R.id.item_smallbtn_remote -> {
                ToastUtils.show("远程问诊")
            }
            R.id.item_smallbtn_bespeak -> {
                ToastUtils.show("预约门诊")
            }

        }


    }


    override fun onClick(p0: View?) {
        when (p0?.id) {
            R.id.findDoctorByAraea -> {
                val bundleOne = Bundle()
                bundleOne.putString(Constant.FINDDOCTORTITLE, "按城市找医生")
                bundleOne.putString(Constant.FINDDOCTORSEARCHHINT, "城市名称")
                startActivity(FindDoctorByAreaActivity::class.java, bundleOne)
            }
            R.id.findDoctotByDisease -> {
                val bundleOne = Bundle()
                bundleOne.putString(Constant.FINDDOCTORTITLE, "按疾病找医生")
                bundleOne.putString(Constant.FINDDOCTORSEARCHHINT, "疾病名称")
                startActivity(FindDoctorByDiseaseActivity::class.java, bundleOne)
            }
            R.id.backLayout -> finish()
        }
    }


    override fun showDoctorList(doctoerList: MutableList<DoctorWrapperEntity.DoctorListEntity.DoctorsEntity>) {
        mDoctorList?.addAll(doctoerList)
        doctorAdapter.notifyDataSetChanged()
        doctorAdapter.loadMoreComplete()
    }

    override fun showDataSuccess(datas: DepatmentWrapperEntity?) {
    }

    override fun reloadActivity() {
    }

    override fun injectTarget(): View = findViewById(R.id.injectView)

    override fun initData() {
        mPresenter.findDepartMent()
        mPresenter.findDoctorList("v1.0", "1", 10, nowCurrent, "1")
        mFlowLayout.setOnTagClickListener { view, _, _ ->
            val textView = view as TextView

            val trim = textView.text.toString()
            if (!TextUtils.isEmpty(trim)) {
                ToastUtils.show(trim)
            }

            false
        }

    }

    override fun initView() {
        conmonTitleTextView.text = getText(R.string.find_doctor_home_title)
        ImmersionBar.with(this)
                .statusBarColor(R.color.colorPrimary)
                .titleBar(R.id.commonTitleLayout)  //使用该属性必须指定状态栏的颜色，不然状态栏透明，很难看
                .init()
        backLayout.setOnClickListener(this)
        headView = LayoutInflater.from(this).inflate(R.layout.find_doctor_header, null)
        mFlowLayout = headView.findViewById(R.id.id_flowlayout)
        headView.findDoctorByAraea.setOnClickListener(this)
        headView.findDoctotByDisease.setOnClickListener(this)
        doctor_list.layoutManager = LinearLayoutManager(this)
        mDoctorList = ArrayList()
        doctorAdapter = FindDoctorDoctorAdapter(mDoctorList)
        doctorAdapter.bindToRecyclerView(doctor_list)
        doctorAdapter.addHeaderView(headView)
        doctor_list.adapter = doctorAdapter
        doctorAdapter.onItemChildClickListener = this

        toSearch.setOnClickListener {

        }





        doctorAdapter.setOnLoadMoreListener {
            if (nowCurrent >= mPageEntity.pages) {
                doctorAdapter.loadMoreEnd()
            } else {
                nowCurrent++
                mPresenter.findDoctorList("v1.0", "1", 10, nowCurrent, "1")
            }
        }

    }

    override fun getContenView() = R.layout.activity_find_docotr_home_page

    override fun showDepartMentList(departments: MutableList<String>) {

        val inflater = LayoutInflater.from(this)
        id_flowlayout.adapter = object : TagAdapter<String>(departments) {
            override fun getView(parent: FlowLayout, position: Int, s: String): View {
                val textView = inflater.inflate(R.layout.flowlayout_sub_text, parent, false) as TextView
                textView.text = s
                return textView
            }

        }
    }

    override fun getPresenter(): FindDoctorPresenter = FindDoctorPresenter()

}
